<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Resources\WxclientController;
use App\Models\Company;
use App\Models\Group;
use App\Http\Requests\AcceptRequest;
use App\Models\AcceptRecord;
use App\Models\Customer;
use App\Models\Project;
use App\Models\Admin;
use App\Models\WxClient;
use App\Services\AdminService;
use App\Services\HelpService;
use App\Services\Repo\ProjectRepo;
use Endroid\QrCode\QrCode;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use App\Http\Requests\ReviewRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class ApiController extends Controller
{

    // 图片上传
    public function upload(Request $request)
    {
        $temp_url = config('filesystems.qrcode.temp');
        if ($request->hasFile('qrcode')) {
            $file = $request->file('qrcode');
            // 图片格式必须为png或者jpg
            $ext = $file->getClientOriginalExtension();
            $pic_arr = ['png', 'jpg', 'jpeg'];
            if (!in_array(strtolower($ext), $pic_arr)) {
                return $this->_error('上传的图片必须是png或者jpg');
            }
            // 上传成功  图片保存在storage/app/temp目录下,并以随机字符串命名,名称返回前台
            if ($file->isValid()) {
                $res = $file->move($temp_url, str_random());
                return $res->getFilename();
            } else {
                return $this->_error('上传失败,请稍后再试');
            }
        } else {
            return $this->_error('二维码图片不存在');
        }
    }

    /**
     * 获取静态配置数据
     * @return array
     */
    function staticress()
    {
        $filter = \Helper::get_filter();
        $ret_data = [];
        //如果传入f参数不合法，返回空数组
        if ($filter) {
            return HelpService::getFileContent($filter, 'app/', '');
        } else {
            return $this->_error('参数不合法');
        }
    }

    /**
     * 获取微信二维码图片
     * @param Request $request
     * @return array
     */
    function qrcode(Request $request)
    {
        //获取图片
        $path = config('filesystems.qrcode.img')
            . '/' . $request->wxClientId . '.png';
        if (file_exists($path)) {
            $img_content = file_get_contents($path);
            header("content-type: image/png");
            echo $img_content;
        } else {
            echo '该微信号没有二维码图片';
        }
        exit;
    }

    /**
     * 生成微信登录二维码
     * @param $uuid
     */
    function show_qrcode($uuid)
    {
        $url_content = "https://login.weixin.qq.com/l/$uuid";
        header('content-type: image/png');
        $qrCode = new QrCode();
        $qrCode
            ->setText($url_content)
            ->setSize(200)
            ->setPadding(1)
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
            ->setImageType('png')
            ->render();
        exit;
    }

    function projects() {
        $data = Project::select(['_id', 'title'])->get();
        $total = count($data);
        return compact('total', 'data');
    }
}

